package net.keebook.webapp.util;

import java.io.IOException;
import java.text.DateFormat;
import java.text.SimpleDateFormat;
import java.util.Calendar;
import java.util.GregorianCalendar;
import java.util.Locale;
import java.util.TimeZone;

import javax.servlet.Filter;
import javax.servlet.FilterChain;
import javax.servlet.FilterConfig;
import javax.servlet.ServletException;
import javax.servlet.ServletRequest;
import javax.servlet.ServletResponse;
import javax.servlet.http.HttpServletResponse;

public class CacheFilter implements Filter {
	private DateFormat httpDateFormat;
	
    public CacheFilter() {
    	httpDateFormat = new SimpleDateFormat("EEE, dd MMM yyyy HH:mm:ss z", Locale.US);
        httpDateFormat.setTimeZone(TimeZone.getTimeZone("GMT"));
    }

	public void destroy() {
	}

	public void doFilter(ServletRequest request, ServletResponse response, FilterChain chain) throws IOException, ServletException {
		Calendar cal = new GregorianCalendar();
		cal.add(Calendar.SECOND, 60*60*24*7); // 7 days
		((HttpServletResponse)response).setHeader("Expires", getHttpDateFormat().format(cal.getTime()));
		chain.doFilter(request, response);
	}

	public void init(FilterConfig fConfig) throws ServletException {
		
	}

	public DateFormat getHttpDateFormat() {
		return httpDateFormat;
	}

	public void setHttpDateFormat(DateFormat httpDateFormat) {
		this.httpDateFormat = httpDateFormat;
	}

}
